/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_lite_tool_host.h"
#include "sle_device_config.h"

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

void OnSleTHostStateChangeCb(SleDeviceState state)
{
    SLE_TEST_TOOL_LOG("[SleHostTestTool] OnSleTHostStateChangeCb state:%s(%hhu)", SleDeviceStateStr(state), state);
}

void TSleEnable(void)
{
    SleErrorCode ret = SleEnable();
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] SleEnable failed, ret:%hhu\r\n", ret);
    }
}

void TSleDisable(void)
{
    SleErrorCode ret = SleDisable();
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] SleDisable failed, ret:%hhu\r\n", ret);
    }
}

void TGetSleState(void)
{
    SleDeviceState sleHostState;
    SleErrorCode ret = GetSleState(&sleHostState);
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] SleGetSleState failed, ret:%hhu\r\n", ret);
        return;
    }
    SLE_TEST_TOOL_LOG("[SleHostTestTool] SleGetSleState state:%s(%hhu)", SleDeviceStateStr(sleHostState), sleHostState);
}

void TGetHostAddress(void)
{
    SleDeviceAddress hostAddr;
    hostAddr.addrType = SLE_PUBLIC_ADDRESS_TYPE;
    SleErrorCode ret = GetHostAddress(&hostAddr);
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] GetHostAddress(public) failed, ret:%hhu\r\n", ret);
        return;
    }
    printf("[SleHostTestTool] GetHostAddress addr(public)"MACSTR"\r\n", MAC2STR(hostAddr.addr));
    hostAddr.addrType = SLE_RANDOM_ADDRESS_TYPE;
    ret = GetHostAddress(&hostAddr);
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] GetHostAddress(random) failed, ret:%hhu\r\n", ret);
        return;
    }
    printf("[SleHostTestTool] GetHostAddress addr(random)"MACSTR"\r\n", MAC2STR(hostAddr.addr));
}

void TSetHostName(void)
{
    char name[SLE_DEVICE_NAME_LEN + 1];
    SleGetInputString("Please input host name", name, sizeof(name));
    SleErrorCode ret = SetHostName((uint8_t*)name, strlen(name));
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] SetHostName failed, ret:%hhu\r\n", ret);
    }
}

void TGetHostName(void)
{
    uint8_t name[SLE_DEVICE_NAME_LEN];
    uint8_t nameLen = SLE_DEVICE_NAME_LEN;
    SleErrorCode ret = GetHostName(name, &nameLen);
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] GetHostName failed, ret:%hhu\r\n", ret);
        return;
    }
    printf("[SleHostTestTool] GetHostName name:%s\r\n", name);
}

void TGetHostConnCfg(void)
{
    SleConnectConfig hostConnParam;
    SleErrorCode ret = GetHostDefaultConnectConfig(&hostConnParam);
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] GetHostConnCfg failed, ret:%hhu\r\n", ret);
        return;
    }
    printf("[SleHostTestTool] connectInterval:%u\r\n", hostConnParam.connectInterval);
    printf("[SleHostTestTool] connectTimeout:%u\r\n", hostConnParam.connectTimeout);
    printf("[SleHostTestTool] connectLatency:%u\r\n", hostConnParam.connectLatency);
    printf("[SleHostTestTool] mtuSize:%u\r\n", hostConnParam.mtuSize);
    printf("[SleHostTestTool] mcs:%hhu\r\n", hostConnParam.mcs);
    printf("[SleHostTestTool] pilotDensity:%hhu\r\n", hostConnParam.pilotDensity);
    printf("[SleHostTestTool] formatType:%hhu\r\n", hostConnParam.formatType);
    printf("[SleHostTestTool] phy:%hhu\r\n", hostConnParam.phy);
    printf("[SleHostTestTool] dataLen:%u\r\n", hostConnParam.dataLen);
}

void SleHostToolHalper(void)
{
    printf("****SleAnnounceTool Command List*****\r\n");
    for (uint16_t i = 0; i < g_hostCmdNum; i++) {
        printf("*     %02d - %-20s     *\r\n", i, g_hostCmdList[i].desc);
    }
    printf("*************************************\r\n");
}

void SleHostToolCmdHandler(int argc, char **argv)
{
    printf("SleHostToolCmdHandler in\r\n");
    //试配uart接口的调用方式
    SleResetEnv();
    //从函数名字之后开始处理参数列表
    SleSetBufEnv(argc, argv, 1);
    int index = SleGetInputNumber("Please input cmd index:\r\n");
    if(index < g_hostCmdNum) {
        printf("cmd: %d\r\n", index);
        printf("Execute: %s\r\n", g_hostCmdList[index].desc);
        g_hostCmdList[index].func();
    }
    if(index != 0) g_hostCmdList[0].func();
}

void TSleUapiNvRead(void)
{
#ifdef __LITEOS_M__
    printf("****TSleUapiNvRead start*****\r\n");

    uint16_t real_len = 0;
    uint8_t data[SLE_NV_CONNECT_ID_DATA_LEN] = {0};

    SleErrorCode ret = SleUapiNvRead(SLE_NV_CONNECT_ID, SLE_NV_CONNECT_ID_DATA_LEN, &real_len, data);
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] NvRead failed, ret:%hhu\r\n", ret);
    }

    printf("recv data length : %d\r\n", real_len);
    for(uint8_t i = 0; i < real_len; i++) {
        printf("value:%hhu\r\n", data[i]);
    }

    printf("****TSleUapiNvRead end*****\r\n");
#else
    printf("****only L0 support*****\r\n");
#endif
}

void TSleUapiNvWrite(void)
{
#ifdef __LITEOS_M__
    printf("****TSleUapiNvWrite start*****\r\n");

    SleConnectModeConfig connect = {
        .workMode = 4,
        .stepCount = 3,
        .stepPeriod = 2,
        .configMode = 1,
    };

    uint8_t dataLen = 0;
    uint8_t data[sizeof(SleConnectModeConfig)] = {0};
    data[dataLen++] = connect.workMode;
    data[dataLen++] = connect.stepCount;
    data[dataLen++] = connect.stepCount << 8;
    data[dataLen++] = connect.stepPeriod;
    data[dataLen++] = connect.stepPeriod << 8;
    data[dataLen++] = connect.configMode;


    SleErrorCode ret = SleUapiNvWrite(SLE_NV_CONNECT_ID, data, dataLen);
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] NvWrite failed, ret:%hhu\r\n", ret);
    }

    printf("****TSleUapiNvWrite end*****\r\n");
#else
    printf("****only L0 support*****\r\n");
#endif
}


#ifdef __cplusplus
}
#endif // __cplusplus
